package com.salelogistics.sale.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.salelogistics.sale.domain.dto.DeliveryPlanDto;
import com.salelogistics.sale.domain.vo.DeliveryPlanVo;
import com.salelogistics.system.api.RemoteBasBuildUnitService;
import com.salelogistics.system.api.RemoteBasContractService;
import com.salelogistics.system.api.RemoteBasMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.salelogistics.common.log.annotation.Log;
import com.salelogistics.common.log.enums.BusinessType;
import com.salelogistics.common.security.annotation.RequiresPermissions;
import com.salelogistics.sale.domain.SmsDeliveryPlan;
import com.salelogistics.sale.service.ISmsDeliveryPlanService;
import com.salelogistics.common.core.web.controller.BaseController;
import com.salelogistics.common.core.web.domain.AjaxResult;
import com.salelogistics.common.core.utils.poi.ExcelUtil;
import com.salelogistics.common.core.web.page.TableDataInfo;

/**
 * 月配送计划Controller
 *
 * @author Hu Kangying
 * @date 2023-07-13
 */
@RestController
@RequestMapping("/delivery")
public class SmsDeliveryPlanController extends BaseController {
    @Resource
    private RemoteBasBuildUnitService remoteBasBuildUnitService;
    @Resource
    private RemoteBasContractService remoteBasContractService;
    @Resource
    private RemoteBasMaterialService remoteBasMaterialService;

    //基于构造器完成依赖注入（SpringBoot4.0版本上可以不写@Autowired）
    private final ISmsDeliveryPlanService deliveryPlanService;
    public SmsDeliveryPlanController(ISmsDeliveryPlanService deliveryPlanService) {
        this.deliveryPlanService = deliveryPlanService;
    }

    /**
     * 查询月配送计划列表
     */
    @RequiresPermissions("sale:delivery:list")
    @GetMapping("/list")
    public TableDataInfo listWithDetails(SmsDeliveryPlan smsDeliveryPlan) {
        startPage();
        List<DeliveryPlanVo> list = deliveryPlanService.selectDeliveryPlanList(smsDeliveryPlan);
        return getDataTable(list);
    }

    /**
     * 获取材料信息
     *
     * @return
     */
    @GetMapping("/material/list")
    public TableDataInfo getMaterialList() {
        TableDataInfo tableDataInfo = remoteBasMaterialService.selectBasMaterialList(null);
        logger.info("Material list:" + tableDataInfo.getRows() + " " + tableDataInfo.getMsg());
        return tableDataInfo;
    }

    /**
     * 获取合同信息
     *
     * @return
     */
    @GetMapping("/contract/list")
    public TableDataInfo getContractList() {
        TableDataInfo tableDataInfo = remoteBasContractService.selectBasContractList(null);
        return tableDataInfo;
    }

    /**
     * 导出月配送计划列表
     */
    @RequiresPermissions("sale:delivery:export")
    @Log(title = "月配送计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmsDeliveryPlan smsDeliveryPlan) {
        List<SmsDeliveryPlan> list = deliveryPlanService.selectSmsDeliveryPlanList(smsDeliveryPlan);
        ExcelUtil<SmsDeliveryPlan> util = new ExcelUtil<SmsDeliveryPlan>(SmsDeliveryPlan.class);
        util.exportExcel(response, list, "月配送计划数据");
    }

    /**
     * 获取月配送计划详细信息
     */
    @RequiresPermissions("sale:delivery:query")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") Long planId) {
        return success(deliveryPlanService.selectSmsDeliveryPlanByPlanId(planId));
    }

    /**
     * 审核月计划
     */
    @RequiresPermissions("sale:delivery:query")
    @PutMapping(value = "/process")
    public AjaxResult processPlan(@RequestBody Long planId) {
        logger.info("审核月计划单，传入的计划id为：" + planId);
        return success(deliveryPlanService.processDeliveryPlan(planId, true));
    }

    /**
     * 反审核月计划
     */
    @RequiresPermissions("sale:delivery:process")
    @PutMapping(value = "/reverse/process")
    public AjaxResult reverseProcessPlan(@RequestBody Long planId) {
        logger.info("反审核月计划单，传入的计划id为：" + planId);
        return success(deliveryPlanService.processDeliveryPlan(planId, false));
    }

    /**
     * 新增月配送计划(只有基本信息，不包括材料信息)
     */
    @RequiresPermissions("sale:delivery:add")
    @Log(title = "月配送计划", businessType = BusinessType.INSERT)
    @PostMapping("/normal")
    public AjaxResult add(@RequestBody SmsDeliveryPlan smsDeliveryPlan) {
        return toAjax(deliveryPlanService.insertSmsDeliveryPlan(smsDeliveryPlan));
    }

    /**
     * 新增月配送计划(携带材料信息)
     */
    @RequiresPermissions("sale:delivery:add")
    @Log(title = "月配送计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add2(@RequestBody DeliveryPlanDto deliveryPlanDto) {
        return toAjax(deliveryPlanService.insertSmsDeliveryPlanWithDetails(deliveryPlanDto));
    }

    /**
     * 修改月配送计划
     */
    @RequiresPermissions("sale:delivery:edit")
    @Log(title = "月配送计划", businessType = BusinessType.UPDATE)
    @PutMapping("/normal")
    public AjaxResult edit(@RequestBody SmsDeliveryPlan smsDeliveryPlan) {
        return toAjax(deliveryPlanService.updateSmsDeliveryPlan(smsDeliveryPlan));
    }

    /**
     * 修改月配送计划(携带材料信息)
     */
    @RequiresPermissions("sale:delivery:edit")
    @Log(title = "月配送计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editWithDetails(@RequestBody DeliveryPlanDto deliveryPlanDto) {
        return toAjax(deliveryPlanService.updateSmsDeliveryPlanWithDetails(deliveryPlanDto));
    }

    /**
     * 删除月配送计划
     */
    @RequiresPermissions("sale:delivery:remove")
    @Log(title = "月配送计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds) {
        return toAjax(deliveryPlanService.deleteSmsDeliveryPlanByPlanIds(planIds));
    }

    /**
     * 新增分解计划（批次计划）
     */
    @PostMapping("/save/batch")
    public AjaxResult saveBatchPlan(@RequestBody DeliveryPlanDto deliveryPlanDto){
        int i = deliveryPlanService.saveBatchPlan(deliveryPlanDto);
        if (i == 0){
            return AjaxResult.error("新增批次计划出错");
        }
        return AjaxResult.success();
    }
}
